package com.starblink.android.common.web.dialog

import android.view.View
import com.starblink.android.basic.bridge.BridgeCommand
import com.starblink.android.basic.bridge.BridgeUtil
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.android.common.R
import com.starblink.android.common.databinding.DialogWebMoreBinding
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.util.ActivityStackManage

class WebMoreDialog(
    val isProduct: Boolean = true,
    val subOrNot: Boolean = false,
    val hasPriceCheck: Boolean = false,
    val idIsWrong: Boolean = false,
    private val clickFollowStore: (view: View) -> Unit,
    private val clickShare: () -> Unit,
    private val findSimilar: () -> Unit,
    private val priceCheck: () -> Unit,
    private val copyLink: () -> Unit,
    private val toWishList: () -> Unit
) : BaseBottomSheetFragment() {
    lateinit var mBinding: DialogWebMoreBinding
    override fun getBindingRootView(): View {
        mBinding = DialogWebMoreBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initView() {
        if (isProduct) {
            mBinding.llSimilar.visibleOrGone(true)
            mBinding.vDivider.visibleOrGone(true)
            mBinding.llPriceCheck.visibleOrGone(true)
            mBinding.tvPriceCheck.setTextColor(
                if (hasPriceCheck) color(com.starblink.basic.style.R.color.gu_text_primary) else color(
                    com.starblink.basic.style.R.color.gu_text_disabled
                )
            )
            mBinding.ivPrice.setImageResource(if (hasPriceCheck) R.mipmap.ic_web_dia_price else R.mipmap.ic_web_dia_price_2)
        }
        if (idIsWrong){
            mBinding.llStore.visibleOrGone(false)
            mBinding.vDivider2.visibleOrGone(false)
        }
        followStore(subOrNot)
        handleVM()
    }


    fun handleVM() {
        mBinding.close.setOnClickListener { dismiss() }
        mBinding.llWishlist.setOnClickListener {
            //跳转心愿单
            dismiss()
            ActivityStackManage.finishAllActivity()
            SkStorage.put(UserDataCenter.HOME_TAB_INDEX_NOSPM, true)
            BridgeUtil.sendCommonData(BridgeCommand.Common.toHomeWish, "") //状态同步给H5
            toWishList.invoke()
        }
        mBinding.llStore.setOnClickListener {
            //Follow店铺
            clickFollowStore.invoke(it)
            dismiss()
        }
        mBinding.llShare.setOnClickListener {
            //分享
            clickShare.invoke()
            dismiss()
        }
        mBinding.llSimilar.setOnClickListener {
            //找相似
            findSimilar.invoke()
            dismiss()
        }
        mBinding.llPriceCheck.setOnClickListener {
            //比价
            priceCheck.invoke()
            dismiss()
        }
        mBinding.llCopy.setOnClickListener {
            //复制链接
            copyLink.invoke()
            dismiss()
        }
    }

    private fun followStore(subOrNot: Boolean) {
        mBinding.tvFollowTip.text = if (subOrNot) "Unfollow the store" else "Follow the store"
        mBinding.ivFollowTip.setImageResource(if (subOrNot) R.mipmap.ic_web_dia_store_added else R.mipmap.ic_web_dia_store)
    }
}